// pages/xjrw/xjrw.js
const utils = require('../../utils/activity_util.js')
// 初始化日期模态框数据
let date = new Date();
let years = [];
let months = [];
let days = [];
let hours = [];
let minutes = [];

for (let i = date.getFullYear()-10; i <= (date.getFullYear()+30); i++) {
  years.push(i + "年")
}
for (let i = 1; i <= 12; i++) {
  months.push(i + "月")
}
for (let i = 1; i <= 31; i++) {
  days.push(i + "日")
}
for (let i = 0; i <= 23; i++) {
  hours.push(i+"")
}
for (let i = 0; i <= 59; i++) {
  minutes.push(i + "")
}


import { createTlTask } from '../../api/tlTask'
import { getTlCompanys,getTlCompanysByCode } from '../../api/tlCompany'
import { getTbPersonalsByCompanyCode } from '../../api/home'

import { getUserInfoByPhone } from '../../api/tbPersonal'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    index: 0,
    array: ['企业正门及值班室', '企业其他进出口', '企业消防设施', '企业危化品存储位置','企业食堂','企业内部男工宿舍','企业内部女工宿舍','企业监控值班室','企业施工区'],
//     1 企业正门及值班室
// 2 企业其他进出口
// 3 企业消防设施
// 4 企业危化品存储位置
// 5 企业食堂
// 6 企业内部男工宿舍
// 7 企业内部女工宿舍
// 8 企业监控值班室
// 9 企业施工区
    sendtypelist:['图片','视频'],
    sendtypeflag:0,


    con:"",    // 输入的文字参数1


    remind:"",    //  参数6
    searchname:"", //模糊企业后期覆值
    comlist:[],
    complist:[],
    executorlist:"",
    executor:"",
    executorname:"",
    executoflag:0,
    isselect:false,

    Minepostion:'',// 索引+1  //查询位置 参数3

    companyInput:'' ,//输入模糊企业     模糊输入的值    
    param1:'',//辖区标识



    changefalg:false,
    value: [0, 1, 1, 1, 1],
    openflag: true,  //1日期控件显示  2控件滚动选择 底部页面不滚动
    years: years,   //时间可选范围模态框数据
    months: months,
    days: days,
    hours:hours,
    minutes:minutes,
    year:'',  //时间值
    month:'',
    day:'',
    hour:'',
    minute:'',


    starttime: utils.getobjDate(),   //参数 截止时间
  },  
////

  tap(ev) {

    // 根据选择项目  传去对应数据  根据开始结束时间获取索引  设置面板默认数据

    let value = [2019,0,0,0,0];
    let arr=[];
      arr = utils.getarrWithtime(this.data.starttime);

    const { years, months, days, hours, minutes, openflag } = this.data;
    //根据arr  数据索引
    value[0] = years.indexOf(arr[0]+'年');
    value[1] = months.indexOf(arr[1] + '月');
    value[2] = days.indexOf(arr[2] + '日');
    value[3] = hours.indexOf(arr[3]);
    value[4] = minutes.indexOf(arr[4]);

    this.setData({
      value,
      openflag: false,
      years,   //日期模态框数据
      months,
      days,
      hours,
      minutes,
    })

  },
  // 取消
  canslebtn (){

    this.setData({
      openflag: true,
      changefalg: false,
    })
  },
  // 确定  如果不选择那么默认重置
  closebtn () {

    this.setData({
      openflag: true,
    })
    const { curindex, year, month, day, hour, minute } = this.data;

    if (this.data.changefalg){
      let starttime = utils.getDate(year, month, day, hour, minute)
      console.log('starttime', starttime)
        this.setData({
          starttime,
          changefalg:false,
        })
    }
  },

  bindChange: function (ev) {
    
    const e = ev;
    let val = e.detail.value;

    const year = this.data.years[val[0]];
    const month = this.data.months[val[1]];
    const day = this.data.days[val[2]];
    const hour = this.data.hours[val[3]];
    const minute = this.data.minutes[val[4]];

    //如果点击月份  那么后面日跟着变换数据
    let days = [];
    const daynum = utils.mGetDate(year.substr(0, year.length - 1), month.substr(0, month.length - 1));
    for (let i = 1; i <= daynum; i++) {
      days.push(i + "日")
    }

    this.setData({
      days,
      year,
      month,
      day,
      hour,
      minute,
      changefalg:true,
    })
  },



//
  upwenjian(e){
    var that = this;
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      success(res) {
        console.log(res)
        // var filename = res.tempFiles[0].name
        // console.info(filename);
        // that.setData({ filename: filename });


        // wx.uploadFile({
        //   url: app.globalData._server + "/test/object/upload",
        //   filePath: res.tempFiles[0].path,
        //   name: 'uploadFile',
        //   success(res) {
        //     //json字符串 需用JSON.parse 转
        //     console.log(res)
        //   }
        // })



      }
    });
  },


  sureexecutor(e) {
    this.setData({
      executoflag: 0
    })
  },


  quitexecutor(e){
    this.setData({
      executoflag:0
    })
  },


  checkboxChange1(e) {
    var exe = ""
    var length = e.detail.value.length
    e.detail.value.forEach((e,i)=>{
      if(i = length){
        exe = exe + e
        
      }else{
        exe = exe + e + ','
      }
    })
    console.log(exe)
    this.setData({
      executor:exe
      
    })
  
  },


  checkboxChange(e){
    var length = e.detail.value.length
    var that = this
    var code = e.detail.value
      let params = {
        'companyCode': code
      }
      console.log(code)
      getTbPersonalsByCompanyCode(params).then(res => {
        var executor = ""
        var executorname = ""
        console.log(res)
        if(res.length > 0){
            executor = executor + res[0].code + ",";
            executorname = executorname + res[0].name + ",";

        }
        
        that.setData({
          executorlist: res,
          executorname: executorname
        })
    })
  },


  coninput(e){
    this.setData({
      con: e.detail.text
    })
  },
  sendtypeclick(e){
    this.setData({
      sendtypeflag:e.target.dataset.index
    })
  },
  userremindInput(e){
    this.setData({
      remind: e.detail.value
    })
  },
  userserachInput(e){
    debugger;
    this.setData({
      searchname: e.detail.value
    })
  },
  searchAgain(e){
    var that = this;
    debugger;
    let data = {
      'searchName': searchname
    }
    console.log(searchName)
    getTlCompanys(data).then(res => {
      that.setData({
        comlist: res
      })
      console.log(res)
    })
  },
  register(e){
    var that = this

     
    let data = {
      "name": name,
      "index": index,
      "array": array,
      "sendtypeflag": sendtypeflag,
      "remind": remind,
      "con": con,
      "executor": executor
    }
    console.log(data)
    return false
    createTlTask(data).then(res => {
      console.log(res)
    })
  },


  getCompanyInput(e){
    this.setData({
      companyInput: e.detail.value
    })
  },
  bindMinepostionChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      Minepostion: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    let data = {}

    
    // getTlCompanys(data).then(res => {
    //   that.setData({
    //     comlist:res
    //   })
    //   // console.log(res)
    // })

    if(wx.getStorageSync('phone')){
      let data={
        telephone :wx.getStorageSync('phone')
      }
      getUserInfoByPhone(data).then(res => {
        console.log(res)
        // console.log(res)
        // app.globalData.userInfo = res
        this.setData({
          param1:res.tbRolePersonalRel.param1
        })
      })
    }
    
 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  bindPicker: function (e) {
    console.log(1)
    //查询模糊企业列表弹框

    let data = {
      companyName:this.data.companyInput,
      districtCode:this.data.param1,
    }

    
    getTlCompanys(data).then(res => {
      console.log(res)
      if(res.length>0){
        this.setData({
          comlist:res,
          executoflag: 1
        })
      }else{
        wx.showToast({
          title: '未查到相关企业',
          icon: 'loading',
          duration: 1500
        })
      }
      
      // console.log(res)
    })


  },
  // bindPickerChange: function (e) {
  //   console.log(1)
  //   this.setData({
  //     executoflag: 1
  //   })
  // },
})